/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bddfirstproject.view;

import bddfirstproject.MainController;
import bddfirstproject.controller.ThemeDAL;
import bddfirstproject.model.Theme;
import bddfirstproject.view.renderer.ListeThemeRenderer;
import java.util.ArrayList;
import javax.swing.ListModel;

/**
 *
 * @author epulapp
 */
public class ThemeListView extends NamedJPanel {
    
    protected static final String title = "Selection thème";

    /**
     * Creates new form ThemeListView
     */
    public ThemeListView() {
        
        initComponents();
        
        //Récupération de la liste des thèmes
        ListeTheme.setCellRenderer(new ListeThemeRenderer());
        ArrayList<Theme> listeTheme = ThemeDAL.getAllThemes();
        ListeTheme.setListData(listeTheme.toArray());
        ListeTheme.setSelectedIndex(0);

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        ListTitle = new javax.swing.JLabel();
        jListThemeListe = new javax.swing.JScrollPane();
        ListeTheme = new javax.swing.JList();
        btnRetour = new javax.swing.JButton();
        btnValiderTheme = new javax.swing.JButton();

        ListTitle.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        ListTitle.setText("Liste des thèmes disponibles");

        ListeTheme.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        ListeTheme.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                ListeThemeMouseClicked(evt);
            }
        });
        ListeTheme.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                ListeThemeValueChanged(evt);
            }
        });
        jListThemeListe.setViewportView(ListeTheme);

        btnRetour.setText("Retour");
        btnRetour.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRetourActionPerformed(evt);
            }
        });

        btnValiderTheme.setText("Liste des articles");
        btnValiderTheme.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnValiderThemeActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jListThemeListe, javax.swing.GroupLayout.DEFAULT_SIZE, 387, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnRetour, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnValiderTheme, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(ListTitle, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(ListTitle)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jListThemeListe, javax.swing.GroupLayout.DEFAULT_SIZE, 253, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnValiderTheme)
                    .addComponent(btnRetour))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnRetourActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRetourActionPerformed
        MainController mainController = MainController.getInstance();
        mainController.setCurrentView(new ConnectionView());
        mainController.setCurrentAuteur(null);
        
    }//GEN-LAST:event_btnRetourActionPerformed

    private void btnValiderThemeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnValiderThemeActionPerformed
        MainController mainController = MainController.getInstance();
        mainController.setCurrentTheme((Theme)ListeTheme.getSelectedValue());
        mainController.setCurrentView(new ArticlesView());
    }//GEN-LAST:event_btnValiderThemeActionPerformed

    private void ListeThemeValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_ListeThemeValueChanged
        if(ListeTheme.getSelectedValue() == null) btnValiderTheme.setEnabled(false);
        else btnValiderTheme.setEnabled(true);
    }//GEN-LAST:event_ListeThemeValueChanged

    private void ListeThemeMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ListeThemeMouseClicked
     if(evt.getClickCount() == 2)
     {
        int index = ListeTheme.locationToIndex(evt.getPoint());
        ListModel dataListModel = ListeTheme.getModel();
        Object item = dataListModel.getElementAt(index);;
        ListeTheme.ensureIndexIsVisible(index);
        System.out.println("Double clicked on " + item);
        
        //Action à effectuer au double click
        MainController mainController = MainController.getInstance();
        mainController.setCurrentTheme((Theme)ListeTheme.getSelectedValue());
        mainController.setCurrentView(new ArticlesView());
     }
    }//GEN-LAST:event_ListeThemeMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel ListTitle;
    private javax.swing.JList ListeTheme;
    private javax.swing.JButton btnRetour;
    private javax.swing.JButton btnValiderTheme;
    private javax.swing.JScrollPane jListThemeListe;
    // End of variables declaration//GEN-END:variables

    @Override
    public String getTitle() {
        return title;
    }
    
}
